﻿using GluePathModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TableModule;

namespace GluePathActioner
{
    public static class GluePathAction
    {
        public static bool GlueStart {get;set;}
        public static GeneralLib.HiPerfTimer glueTimer = new GeneralLib.HiPerfTimer();

        public static double dTotalTimeGlue;
        public static void GluePathActionStart(TableModule.TableActioner tableActioner,string strGroupName,int iOutPutChannal)
        {
            GlueStart = true;
            glueTimer.Start();
            dTotalTimeGlue = 0;
            TableDataPosition OffSet = TableManager.tableRunner.GetPosition("偏移设定");
            TableDataPosition tablePosStardard = TableManager.tableRunner.GetPosition("针头工具位置");
            double dZToBelt = tablePosStardard.ZPos;

            List<GluePathModule.MotionItemBase> motionItems = GluePathModule.GluePathManager.GetMotionList(strGroupName, OffSet.dZPos);
            #region 插入一个点让轴移动过到点胶上方
            MotionItemLine firstLineItem = motionItems[0] as MotionItemLine;
            PointData pointData = new PointData(firstLineItem.Point);
            SpdData spdData = new SpdData(firstLineItem.spdData);
            pointData.Z = dZToBelt-TableManager.tableRunner.WhereTable().dZPos;
            spdData.dMoveSpd = tableActioner.tableDateGlabal.dicTableDataSpeed["HighSpd"].dMoveSpeed;
            MotionItemLine motionItemLineAdd = new MotionItemLine(spdData, pointData);
            motionItems.Insert(0, motionItemLineAdd);

            PointData pointData1 = new PointData(firstLineItem.Point);
            SpdData spdData1 = new SpdData(firstLineItem.spdData);
            pointData1.Z = firstLineItem.Point.Z;
            spdData1.dMoveSpd = tableActioner.tableDateGlabal.dicTableDataSpeed["LowSpd"].dMoveSpeed;
            MotionItemLine motionItemLineAdd1 = new MotionItemLine(spdData1, pointData1);
            motionItems.Insert(1, motionItemLineAdd1);
            #endregion
            if (motionItems == null)
                return;
            
            bool bUAxisUsed = tableActioner.tableSetting.tableAxisSettings[(int)AxisName.U].bUsed;
            //tableActioner.SetTool("Nozzle");
            tableActioner.SetTool("Default");
            tableActioner.InitCure();
            for (int i = 0; i < motionItems.Count; i++)
            {
                if (motionItems[i] is GluePathModule.MotionItemArc)
                {
                    GluePathModule.MotionItemArc motionItemArc = motionItems[i] as GluePathModule.MotionItemArc;
                    SetSpdForCure(tableActioner, motionItems[i].spdData);
                    TableModule.TableDataPosition tableDataPositionStart = new TableModule.TableDataPosition();
                    TableModule.TableDataPosition tableDataPositionMid = new TableModule.TableDataPosition();
                    TableModule.TableDataPosition tableDataPositionEnd = new TableModule.TableDataPosition();
                    tableDataPositionStart.dXPos = motionItemArc.PointStart.X;
                    tableDataPositionStart.dYPos = motionItemArc.PointStart.Y;
                    tableDataPositionStart.dZPos = dZToBelt-motionItemArc.PointStart.Z;
                    tableDataPositionStart.dUPos = motionItemArc.PointStart.U;
                    tableDataPositionMid.dXPos = motionItemArc.PointMid.X;
                    tableDataPositionMid.dYPos = motionItemArc.PointMid.Y;
                    tableDataPositionMid.dZPos = dZToBelt - motionItemArc.PointMid.Z;
                    tableDataPositionMid.dUPos = motionItemArc.PointMid.U;
                    tableDataPositionEnd.dXPos = motionItemArc.PointEnd.X;
                    tableDataPositionEnd.dYPos = motionItemArc.PointEnd.Y;
                    tableDataPositionEnd.dZPos = dZToBelt - motionItemArc.PointEnd.Z;
                    tableDataPositionEnd.dUPos = motionItemArc.PointEnd.U;
                    if (bUAxisUsed == false)
                    {
                        motionItemArc.SweeAngle = 0;
                    } 
                    tableActioner.PushArcToCure(tableDataPositionStart, tableDataPositionMid, tableDataPositionEnd, motionItemArc.IsCW, motionItemArc.SweeAngle);
                }
                if (motionItems[i] is GluePathModule.MotionItemCircle)
                {
                    GluePathModule.MotionItemCircle motionItemCircle = motionItems[i] as GluePathModule.MotionItemCircle;
                    SetSpdForCure(tableActioner, motionItems[i].spdData);
                    TableModule.TableDataPosition tableDataPositionStart = new TableModule.TableDataPosition();
                    TableModule.TableDataPosition tableDataPositionCenter = new TableModule.TableDataPosition();
                    
                    tableDataPositionStart.dXPos = motionItemCircle.PointStart.X;
                    tableDataPositionStart.dYPos = motionItemCircle.PointStart.Y;
                    tableDataPositionStart.dZPos = dZToBelt - motionItemCircle.PointStart.Z;
                    tableDataPositionStart.dUPos = motionItemCircle.PointStart.U;
                    tableDataPositionCenter.dXPos = motionItemCircle.PointCenter.X;
                    tableDataPositionCenter.dYPos = motionItemCircle.PointCenter.Y;
                    tableDataPositionCenter.dZPos = dZToBelt - motionItemCircle.PointCenter.Z;
                    tableDataPositionCenter.dUPos = motionItemCircle.PointCenter.U;
                    double dAngle = motionItemCircle.Angle;
                    if (bUAxisUsed == false)
                    {
                        dAngle = 0;
                    }
                    tableActioner.PushArcToCure(tableDataPositionStart, tableDataPositionCenter, dAngle);
                }
                if (motionItems[i] is GluePathModule.MotionItemDelay)
                {
                    GluePathModule.MotionItemDelay motionItemDelay = motionItems[i] as GluePathModule.MotionItemDelay;
                    tableActioner.PushDelay(motionItemDelay.dDelayTime);
                }
                if (motionItems[i] is GluePathModule.MotionItemLine)
                {

                    GluePathModule.MotionItemLine motionItemLine = motionItems[i] as GluePathModule.MotionItemLine;
                    TableModule.TableDataPosition tableDataPosition = new TableModule.TableDataPosition();
                    SetSpdForCure(tableActioner, motionItems[i].spdData);
                    tableDataPosition.dXPos = motionItemLine.Point.X;
                    tableDataPosition.dYPos = motionItemLine.Point.Y;
                    tableDataPosition.dZPos = dZToBelt - motionItemLine.Point.Z;
                    tableDataPosition.dUPos = motionItemLine.Point.U;
                    tableActioner.PushLineToCure(tableDataPosition);
                }
                if (motionItems[i] is GluePathModule.MotionItemOutput)
                {
                    GluePathModule.MotionItemOutput motionItemOut = motionItems[i] as GluePathModule.MotionItemOutput;
                    int iOnOrOff = motionItemOut.On ? 0 : 1;
                    tableActioner.PushExtDo(iOutPutChannal, iOnOrOff);
                }
            }
            tableActioner.StartCure();
            tableActioner.SetTool("Default");

        }
        public static void SetSpdForCure(TableModule.TableActioner tableActioner, GluePathModule.SpdData spdData)
        {
            TableModule.TableDataSpeed tableMotionSpeed = new TableModule.TableDataSpeed();
            tableMotionSpeed.dAcc = spdData.dAcc;
            tableMotionSpeed.dDec = spdData.dDec;
            tableMotionSpeed.dEndSpeed = spdData.dEndSpd;
            tableMotionSpeed.dMoveSpeed = spdData.dMoveSpd;
            tableMotionSpeed.dStartSpeed = spdData.dStartSpd;
            tableActioner.SetSpd(tableMotionSpeed);
        }

        public static void GluePathActionStop()
        {
            GlueStart = false;
            ProjectModule.ProjectManager.ProjectSetting.SingleProductGlueTime = dTotalTimeGlue;
            
        }
    }
}
